package chai.ning.hish;

public class Monkey { // 当没有显式指定父类时，父类就是java.lang.Object类

	private String name;
	private int age;
	private char gender;
	private boolean married;
	
	public Monkey(String name, int age, char gender, boolean married) {
		super(); // 调用父类无参构造
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.married = married;
	}

	@Override
	public String toString() {
		return "Monkey[" + name + ", " + gender + ", " + age + ", " + (married?"已婚":"未婚") + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31; // 为什么要用质数? 那么多质数为什么非要用31 ?
		int result = 1;
		result = prime * result + age;
		result = prime * result + gender;
		result = prime * result + (married ? 1231 : 1237);
		// name 是  String 类型，所以求 name 的 哈希码值 需要借助于 String 类重写的 hashCode 方法
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	
	// 建议，重写 hashCode时要同时重写 equals，同样 重写 equals 时要同时重写 hashCode
	// 另外，在 equals 中参与比较的 字段 也是 hashCode中生成哈希码值时所使用的 字段

	@Override
	public boolean equals( Object o ) {
		
		if( o instanceof Monkey ) {
			Monkey other = (Monkey)o;
			if( this.age == other.age && 
				this.gender == other.gender && 
				this.married == other.married && 
				this.name.equals( other.name ) ) {
				return true ;
			}
		}
		
		return false ;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

	public boolean isMarried() {
		return married;
	}

	public void setMarried(boolean married) {
		this.married = married;
	}

}
